package com.ekuaitu.kuaitustaff.utils.bos;

import android.content.Context;

import com.baidubce.BceClientException;
import com.baidubce.BceServiceException;
import com.baidubce.auth.BceCredentials;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.PutObjectResponse;
import com.ekuaitu.kuaitustaff.utils.Logger;

import java.io.File;

/**
 * Created by michaelpi on 2018/6/20.
 */

public class UpdateFile {
    private Context mContext;
    private static UpdateFile instance;

    public static UpdateFile getInstance() {
        if (instance == null) {
            instance = new UpdateFile();
        }
        return instance;
    }

    public UpdateFile() {
    }

    public void uploadPicToBos(Context context) {
        mContext = context;
        // 1. get pic params from ui: file name, file location uri etc
        // 2. send params to app server and get sts, bucket name and region
        // 3. upload selected pic to bos with sts etc, which bos client needs
        File file = new File(mContext.getExternalFilesDir("Log").getPath() + File.separator + "logs.txt");
        Logger.i("开始上传" + file.getAbsolutePath());
        if (!file.exists()) {
            //ToastUtils.makeText(mContext, "上传失败", ToastUtils.LENGTH_LONG).show();
            Logger.i("准备上传没有文件" + file.getAbsolutePath());
            return;
        }
        Logger.i("准备上传1" + file.getAbsolutePath());
        BceCredentials credentials = null;
        credentials = new DefaultBceCredentials("233a6499eee24777826d0cd04815f419", "74a359feb1464025aa92cfbcd90d6c58");
        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(credentials);
        Logger.i("准备上传2" + file.getAbsolutePath());
        final BosClient client = new BosClient(config);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //上传Object
                    PutObjectResponse putObjectFromFileResponse = client.putObject("difulogs", file.getName(), file);
                    Logger.i("Error ErrorType: " + "上传成功");
                    //上传成功
                    updateSuccess(file);
                } catch (BceServiceException e) {
                    Logger.i("准备上传失败" + file.getAbsolutePath());
                    Logger.i("Error ErrorCode: " + e.getErrorCode());
                    Logger.i("Error RequestId: " + e.getRequestId());
                    Logger.i("Error StatusCode: " + e.getStatusCode());
                    Logger.i("Error Message: " + e.getMessage());
                    Logger.i("Error ErrorType: " + e.getErrorType());
                    // finished uploading file, send a message to inform ui
                    //上传失败
                } catch (BceClientException e) {
                    Logger.i("Error Message: " + e.getMessage());
                }

            }
        }).start();

    }

    private void updateSuccess(File file) {
        if (file.exists()) file.delete();
    }


    /*
     * Java文件操作 获取不带扩展名的文件名
     */
    public String getFileNameNoEx(File nameFile) {
        String filename = nameFile.getName();
        if (filename.length() > 0) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }
}
